import { lobby as _ } from './UIConfig';
import { BaseUIRoot } from '../../scripts/libs/ui/BaseUIRoot';
import { _decorator, color } from 'cc';
import { lm } from './LobbyManager';
import { FlowStatus } from './Enums';
import { GameKey } from '../../scripts/managers/SubgameConfig';
import { gm } from '../../scripts/managers/GameManager';
const { ccclass, property } = _decorator;

export namespace lobby {

    const config = new Map<FlowStatus, Set<_.PanelKey>>([
        [FlowStatus.Empty, new Set()],
        [FlowStatus.Lobby, new Set([
            _.PanelKey.LobbyPanel
        ])],
    ]);

    const gameConfig = new Map<GameKey, Set<_.PanelKey>>([
        [null, new Set()],
        [GameKey.lobby, new Set()],
        [GameKey.subgame001, new Set([
            _.PanelKey.Subgame001RootPanel
        ])],
        [GameKey.subgame002, new Set([
            _.PanelKey.Subgame002RootPanel
        ])],
    ]);

    @ccclass('UIRoot')
    export class UIRoot extends BaseUIRoot {

        get logColorOverride() { return color().fromHEX('#DDDDEE') };

        getPanelKeyEnum() {
            return _.PanelKey as any;
        }

        willBind() {
            super.willBind();
            lm.uiRoot = this;
        }

        useObserves() {
            return [
                ...super.useObserves(),
                lm.flowStatus.subscribePairWise(pair => {
                    this.statusChangedWithConfig(pair, config);
                }),
                lm.loadingEnabled.subscribeUntilChanged(enabled => enabled ? this.openPanel(_.PanelKey.LoadingPanel as any).subscribe() : this.closePanel(_.PanelKey.LoadingPanel as any).subscribe()),
                gm.curSubgame.subscribePairWise(subgameKey => {
                    this.statusChangedWithConfig(subgameKey, gameConfig);
                }),
                lm.frontTipEnabled.subscribe(enabled => {
                    if (enabled) {
                        this.openPanel(_.PanelKey.FrontTipPanel as any).subscribe(_ => {
                            if (_._ == 'afterOpen') {
                                lm.frontTipReady.value = true;
                            }
                        });
                    } else {
                        this.closePanel(_.PanelKey.FrontTipPanel as any).subscribe()
                    }
                }),
            ];
        }

    }

}